{include file="public/layout"/}
<body data-type="generalComponents">
    <div class="tpl-content-wrapper js-check-wrap">
        <ol class="am-breadcrumb">
            <li><a href="javascript:void(0)" onclick="javascript:parent.openItem('welcome|Index');" class="am-icon-home">首页</a></li>
            <li class="am-active">数据</li>
            <li class="am-active">数据备份</li>
        </ol>
        <div class="tpl-portlet-components">
            <div class="tpl-block">
                <form class="js-ajax-form" method="post">
                    <div class="am-g">
                        <div class="am-u-sm-12 am-u-md-6">
                            <div class="am-btn-toolbar am-vertical-align">
                                <div class="am-btn-group am-btn-group-xs">
                                    {$action|actionList|raw}
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
                <div class="am-g">
                    <div class="am-u-sm-12">
                        <form class="am-form" id="export-form" action="{:url('export')}">
                            <table class="am-table am-table-striped am-table-hover table-main">
                                <thead>
                                <tr>
                                    <th colspan="2">备份类型</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <td>
                                        <div class="am-radio">
                                            <label>
                                                <input type="radio" name="type" value="full" checked onclick="findobj('showtables').style.display='none'">
                                                全部备份
                                            </label>
                                        </div>
                                    </td>
                                    <td>备份数据库所有表</td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="am-radio">
                                            <label>
                                                <input type="radio" name="type" value="stand" onclick="findobj('showtables').style.display='none'">
                                                标准备份（推荐）
                                            </label>
                                        </div>
                                    </td>
                                    <td>备份常用的数据库</td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="am-radio">
                                            <label>
                                                <input type="radio" name="type" value="custom" onclick="findobj('showtables').style.display=''">
                                                自定义备份
                                            </label>
                                        </div>
                                    </td>
                                    <td>根据自行选择备份数据库</td>
                                </tr>
                                </tbody>
                                <tbody id="showtables" style="display: none">
                                    <tr>
                                        <td colspan="2">
                                            <table class="am-table am-table-striped table-main">
                                                <tr>
                                                    <td colspan="4">
                                                        <div class="am-checkbox">
                                                            <label>
                                                                <input type="checkbox" name="chkall" onclick="checkall(this.form, 'tables[]')" id="chkall"> 全选
                                                            </label>
                                                        </div>
                                                    </td>
                                                </tr>
                                                {$html|raw}
                                            </table>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <div class="am-form-group">
                                <div class="am-u-sm-9 am-u-sm-push-3">
                                    <button type="button" class="am-btn am-btn-primary" id="export">开始备份</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script src="__ADMIN__/js/admin.js"></script>
    <script>
        (function($){
            var $form = $("#export-form"), $export = $("#export"), tables
            $export.click(function(){
                var type = $('input[name="type"]:checked').val();
                if (type == 'custom') {
                    if ($("input[name^='tables']:checked").length == 0) {
                        parent.$.windowbox.showAlert('', '请选中要备份的数据表');
                        return false;
                    }
                }
                $export.addClass("disabled");
                $export.html("正在发送备份请求...");
                $.post(
                    $form.attr("action"),
                    $form.serialize(),
                    function(data){
                        if(data.code){
                            tables = data.data.tables;
                            $export.html(data.msg + "开始备份，请不要关闭本页面！");
                            backup(data.data.tab);
                            window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                        } else {
                            parent.$.windowbox.showAlert('', data.msg);
                            $export.removeClass("disabled");
                            $export.html("立即备份");
                        }
                    },
                    "json"
                );
                return false;
            });

            function backup(tab, status){
                status && showmsg(tab.id, "开始备份...(0%)");
                $.get($form.attr("action"), tab, function(data){
                    if(data.code){
                        showmsg(tab.id, data.msg);
                        if(!$.isPlainObject(data.data.tab)){
                            $export.removeClass("disabled");
                            $export.html("备份完成，点击重新备份");
                            window.onbeforeunload = function(){ return null }
                            return;
                        }
                        backup(data.data.tab, tab.id != data.data.tab.id);
                    } else {
                        $export.removeClass("disabled");
                        $export.html("立即备份");
                    }
                }, "json");
            }

            function showmsg(id, msg){
                $form.find("input[value=" + tables[id] + "]").closest("td").find(".info").html(msg);
            }
        })(jQuery);
        function findobj(str)
        {
            return document.getElementById(str);
        }
        function checkall(frm, chk)
        {
            for (i = 0; i < frm.elements.length; i++)
            {
                if (frm.elements[i].name == chk)
                {
                    frm.elements[i].checked = frm.elements['chkall'].checked;
                }
            }
        }
    </script>
</body>
</html>